home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: hearst.acc.Virginia.EDU!maxwell!gcl8a
- From: gcl8a@Virginia.EDU (Gregory Carl Lewin)
- Subject: copy ctor and derived classes
- Message-ID: <DL38zz.50K@Virginia.EDU>
- Organization: University of Virginia
- Date: Fri, 12 Jan 1996 21:57:35 GMT
-
- This may be a silly question, but I can't seem to find the
- answer in any of my reference books:
-
- If I have a base class and a dervied class, and the derived
- class has a copy constructor, how can I invoke the base class's
- copy constructor in the intialization list so the derived class
- doesn't have to deal with all of the little bits of the base
- class?
-
- e.g.
-
- class Base{
- int i;
- public:
- Base(const Base& b) : i(b.i) {}
- }
-
- class Derived : public Base{
- int j;
- public:
- Derived(const Derived& d) : /*tedious i(d.i) */
- j(d.j) {}
- };
-
- assume there is a non-trivial reason to do this sort of thing
- (and that i didn't forget the ; at the end of Base :)
-
- of course, this only works when i is protectted, right? what
- if i is private (which it mistakenly is in my example)?
-
- and what if the inheritance is virtual? does your answer still
- hold?
-
- please respond via email:
-
- GCL8A@virginia.edu
-
- Greg Lewin
-